﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using System.Linq;

    public partial class frmCapNhatDmQuanHuyen : DevExpress.XtraEditors.XtraForm
    {
        int State = 0;

        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        IList<DmQuocGiaEntity> lstDmQuocGiaEntity = null;

        DmTinhThanhBusiness dmTinhThanhBusiness = new DmTinhThanhBusiness();
        IList<DmTinhThanhEntity> lstDmTinhThanhEntity = null;

        DmQuanHuyenBusiness dmQuanHuyenBusiness = new DmQuanHuyenBusiness();
        DmQuanHuyenEntity dmQuanHuyenEntity = null;

        public event EventHandler ButtonClicked;

        void LoadQuocGia()
        {
            lstDmQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            var dmQuocGiaEntity = new DmQuocGiaEntity()
            {
                MaQuocGia = ConstantManagerClinic.sValueMember,
                TenQuocGia = ConstantManagerClinic.sDisplayMember
            };
            lstDmQuocGiaEntity.Insert(0, dmQuocGiaEntity);

            lkQuocGia.Properties.DataSource = lstDmQuocGiaEntity;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadTinhThanh()
        {
            lstDmTinhThanhEntity = dmTinhThanhBusiness.GetDmTinhThanhList();

            if (lstDmTinhThanhEntity.Count > 0 && lkQuocGia.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmTinhThanhEntity = lstDmTinhThanhEntity.Where(n => n.MaQuocGia == lkQuocGia.EditValue.ToString()).ToList();
            }

            var dmTinhThanhEntity = new DmTinhThanhEntity()
            {
                MaTinhThanh = ConstantManagerClinic.sValueMember,
                TenTinhThanh = ConstantManagerClinic.sDisplayMember
            };
            lstDmTinhThanhEntity.Insert(0, dmTinhThanhEntity);

            lkTinhThanh.Properties.DataSource = lstDmTinhThanhEntity;
            lkTinhThanh.Properties.DisplayMember = "TenTinhThanh";
            lkTinhThanh.Properties.ValueMember = "MaTinhThanh";
            lkTinhThanh.EditValue = ConstantManagerClinic.sValueMember;
        }

        public frmCapNhatDmQuanHuyen(int state)
        {
            InitializeComponent();

            State = state;
            LoadQuocGia();
            LoadTinhThanh();
        }

        public frmCapNhatDmQuanHuyen(int state,DmQuanHuyenEntity obj)
        {
            InitializeComponent();

            State = state;
            LoadQuocGia();
            LoadTinhThanh();
            dmQuanHuyenEntity = obj;

            lkQuocGia.EditValue = lstDmTinhThanhEntity.Where(n => n.MaTinhThanh == dmQuanHuyenEntity.MaTinhThanh).FirstOrDefault().MaQuocGia;
            lkTinhThanh.EditValue = dmQuanHuyenEntity.MaTinhThanh;
            txtMaQuanHuyen.Text = dmQuanHuyenEntity.MaQuanHuyen;
            txtTenQuanHuyen.Text = dmQuanHuyenEntity.TenQuanHuyen;
            txtMoTa.Text = dmQuanHuyenEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmQuanHuyenEntity.TrangThaiSuDung;

            txtMaQuanHuyen.Enabled = false;
        }

        private void frmCapNhatDmQuanHuyen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lkQuocGia_EditValueChanged(object sender, EventArgs e)
        {
            LoadTinhThanh();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkQuocGia, "quốc gia") == false) return;
            if (Validation.ValidLookUpEdit(lkTinhThanh, "tỉnh thành") == false) return;
            if (validateManager1.IsValid() == false) return;

            if (Validation.ValidSpaceTextBoxKey(txtMaQuanHuyen, "mã quận huyện") == false) return;

            dmQuanHuyenEntity = new DmQuanHuyenEntity() { 
                MaQuanHuyen = txtMaQuanHuyen.Text,
                TenQuanHuyen= txtTenQuanHuyen.Text,
                MoTa = txtMoTa.Text,
                TrangThaiSuDung = chkTrangThaiSuDung.Checked,
                MaTinhThanh = lkTinhThanh.EditValue.ToString()
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmQuanHuyenBusiness.GetDmQuanHuyen(txtMaQuanHuyen.Text) != null) {
                    if (Validation.ValidPrimaryKey(txtMaQuanHuyen, "mã quận huyện") == false) return;
                }

                dmQuanHuyenBusiness.InsertDmQuanHuyen(dmQuanHuyenEntity);
                Validation.ClearControl(this);
                lkQuocGia.Focus();
            }
            else {
                dmQuanHuyenBusiness.EditDmQuanHuyen(dmQuanHuyenEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (ButtonClicked != null) ButtonClicked(this, e);

        }

        private void frmCapNhatDmQuanHuyen_Load(object sender, EventArgs e)
        {

        }
    }
}